package com.mz.ozy.activity.assets;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.QCDetailAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.QCDetailBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.QCDetailPresenter;
import com.mz.ozy.mvp.view.QCDetailContract;
import com.mz.ozy.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * qc明细
 */
public class QCDetailsActivity extends BaseActivity<QCDetailContract.View, QCDetailPresenter> implements QCDetailContract.View {

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_exchange)
    TextView tvExchange;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private QCDetailAdapter mQCDetailAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<QCDetailBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private int isOpen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qc_details);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initTitle();
        tvTitle.setText(R.string.assets_qc_detail);
        isOpen = getIntent().getIntExtra("isOpen", 1);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mQCDetailAdapter = new QCDetailAdapter(this, mBeanList);
        recyclerview.setAdapter(mQCDetailAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getQCDetail(QCDetailsActivity.this, String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getQCDetail(QCDetailsActivity.this, String.valueOf(pageNo));

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (event.equals(Constants.EventCode.REFRESH_DETAIL_LIST)) {
            smartRefreshLayout.autoRefresh();
        }

    }

    @OnClick({R.id.tv_exchange, R.id.tv_transfer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_exchange:
                Intent intent = new Intent(this, ExchangeActivity.class);
                intent.putExtra("tag", 2);
                startActivity(intent);

                break;
            case R.id.tv_transfer:
                if (isOpen == 1) {
                    startActivity(new Intent(this, TransferActivity.class));

                } else {
                    ToastUtils.showToast(getString(R.string.str_transfer_not_open));

                }


                break;
        }
    }

    @Override
    public void getQCDetailSuccess(QCDetailBean bean) {
        tvBalance.setText(bean.getQcBalance());
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }

        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mQCDetailAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
